package com.binary.markdownrecord.config.template;


import java.io.*;

public class CodeGenerator {
    public final static String bathPath = "D:/3_Work/markdown-record/src/main/java/com/binary/markdownrecord/";
    public final static String baseModelPackage = "com.binary.markdownrecord";
    public final static String templatePath = "config/template/";
    public final static String modelPath = "model/";
    public final static String modelPackage = baseModelPackage + ".model";

    public static String codeReader(String type, String className) {
        StringBuilder sb = new StringBuilder();
        try {
            String name = "EvTemplate" + getPascal(type) + ".java";
            String packageName = baseModelPackage + ".common";
            BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(bathPath + templatePath + name)));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                line = line.replace("EvTemplate", className);
                line = line.replace("evTemplate", className.substring(0, 1).toLowerCase() + className.substring(1, className.length()));
                if (line.startsWith("//import")) {
                    line = line.replace("//", "").replace("${package}", packageName).replace("${model}", modelPackage);
                } else if (line.startsWith(String.format("package %s.config.template;", baseModelPackage))) {
                    line = line.replace(baseModelPackage + ".config.template", packageName + "." + type);
                }
                sb.append(line + "\n");
            }
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

    private static String getPascal(String type) {
        return type.substring(0, 1).toUpperCase() + type.substring(1, type.length());
    }

    public static void writeCode(String type, String className) {
        String name = getPascal(type) + ".java";

        try {
            File writeName = new File(bathPath + "/common/" + type + "/" + className + name);
            // 创建新文件,有同名的文件的话直接覆盖
            writeName.createNewFile();
            try (FileWriter writer = new FileWriter(writeName);
                 BufferedWriter out = new BufferedWriter(writer)
            ) {
                out.write(codeReader(type, className));
                out.flush(); // 把缓存区内容压入文件
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void generator() {
        File modelPackage = new File(bathPath + modelPath);
        File[] files = modelPackage.listFiles();
        for (File file : files) {
            String name = file.getName().replace(".java", "");
            writeCode("controller", name);
            writeCode("service", name);
            writeCode("repository", name);
        }
    }


    public static void main(String[] args) {
        // writeCode("RestTemplateController", "", "");
        generator();
        // System.out.println(codeReader("service","EvPoint"));
    }
}
